﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.Sale;
using RedDragon.RBAC.ErrorCodeEnum;
using T.Common.Domain.Common;
namespace RedDragon.RBAC.API.Application.Common.Storehouse
{
    public class UpdateStorehouseHandler : IRequestHandler<UpdateStorehouseCommand, Result>
    {


        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 工作表仓储
        /// </summary>
        public readonly IBaseRepository<Storehouses> StorehousesRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<UpdateStorehouseHandler> logger;

        public UpdateStorehouseHandler(IMapper mapper, IBaseRepository<Storehouses> StorehousesRepository, ILogger<UpdateStorehouseHandler> logger)
        {
            this.mapper = mapper;
            this.StorehousesRepository = StorehousesRepository;
            this.logger = logger;
        }

        public async Task<Result> Handle(UpdateStorehouseCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Storehouses Storehouses = mapper.Map<Storehouses>(request);
                var res = await StorehousesRepository.Update(Storehouses);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = Storehouses;
                    result.Msg = "入库修改成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "入库修改失败" + ex.Message;
                logger.LogError("入库修改失败" + ex.Message);
            }
            return result;
        }
    }
}
